package com.baosight.qualitycontrol.util;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;

public class DateUtil {
	private static final String SIMPLEDATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	private static final String SIMPLEDATE_FORMAT = "yyyy-MM-dd";
	public static LocalDateTime parseLocalDateTime(String dateString) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern(SIMPLEDATETIME_FORMAT);
		LocalDateTime localDateTime = LocalDateTime.parse(dateString, df);
        return localDateTime;
    }
	
	public static LocalDate parseLocalDate(String dateString) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern(SIMPLEDATE_FORMAT);
		LocalDate localDate = LocalDate.parse(dateString, df);
        return localDate;
    }
	
	
	public static Date parseDate(String date) {
        return parse(date, "yyyy-MM-dd");
    }
	
	public static Date parseTime(String date) {
        return parse(date, "yyyy-MM-dd HH:mm:ss");
    }
	
	public static Date parse(String date, String pattern) {
        try {
            return DateUtils.parseDate(date, pattern);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
	
	public static boolean isOverlapping(Date start1, Date end1, Date start2, Date end2) {
		// 情况一：完全包含
        if (start1.after(start2) && end1.before(end2)) {
            return true;
        }
        // 情况二：前段重合
        else if (start1.before(start2) && end1.after(start2) && end1.before(end2)) {
            return true;
        }
        // 情况三：后段重合
        else if (start1.after(start2) && start1.before(end2) && end1.after(end2)) {
            return true;
        }
        // 情况四：交叉重合
        else if (start1.before(end2) && end1.after(start2)) {
            return true;
        }
        return false;
	}
}
